// pages/home/home.js
import {
  getBookingInfoList,
  getBookingInfo
} from '@/api/shop'
import { getExamAnswerList } from '@/api/task/questionnaire.service';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUri: getApp().globalData.imgUri,
    startime: '',
    endTime: '',
    story: "",
    array: ['2111', '2111', '2111', '2111'],
    dataArr: [],
    dataCount: 0,
    showDetail: false,
    bookInfo: {}, // 预约详情

    currentTab: 1,
    tabs: [
      { key: 1, title: "预约名单" },
      { key: 2, title: "有奖问卷名单" }
    ],
    th: [
      { key: 'id', name: '序号' },
      { key: 'nickName', name: '姓名' },
      { key: 'phonenumber', name: '联系方式' },
      // { key: 'isRedeemed', name: '是否核销', show: [1] },
      { key: 'isRedeemed', name: '是否核销', hidden: true },
    ],
    // thShow: [],
    questionList: [],
    questionDetail: {},
    isShowDialogQ: false
  },

  /** 点击tab */
  onChangeTab(e) {
    console.log("change tab", e.currentTarget.dataset.key);
    const key = e.currentTarget.dataset.key;
    if (key == 1) this.getBookingList();
    else if (key == 2) this.getQuestionList();
    this.setData({ currentTab: e.currentTarget.dataset.key });
  },

  /** 查看问卷详情 */
  showDialogQ(e) {
    const userId = e.currentTarget.dataset.id;
    console.log("userId", userId);
    getExamAnswerList({ userId }).then(res => {
      if (res.code == 200)
        this.setData({
          questionDetail: res.rows.map(item => {
            return {
              ...item,
              answer: item.questionType == 'WDT' ? item.answerContent : item.answerOptions.join(' / ')
            }
          }), isShowDialogQ: true
        });
    });
  },

  /** 关闭问卷详情 */
  hideDialogQ(e) {
    this.setData({ isShowDialogQ: false })
  },

  /** 获取预约名单 */
  getBookingList() {
    let t = this;
    let shopToken = wx.getStorageSync('shopToken');
    getBookingInfoList({
      shopToken: shopToken
    }).then(res => {
      if (res.code == 200) {
        //状态1:待核销 2：已核销 3：已取消 4：已过期
        for (var i = 0; i < res.data.length; i++) {
          if (res.data[i].status == 2) {
            res.data[i].statusStr = "是";
          } else {
            res.data[i].statusStr = "否";
          }
        }
        t.setData({
          dataArr: res.data,
          dataCount: res.data.length
        })
      }
    });
  },


  /** 获取有奖问卷名单 */
  getQuestionList() {
    getExamAnswerList({ userGroup: true }).then(res => {
      if (res.code == 200) {
        this.setData({
          questionList: res.rows.map((item, index) => { return { ...item, id: index + 1 } })
        })
      }
    });
  },

  todetail() {

  },
  bindRegionChange(e) {
    this.setData({
      startime: e.detail.value
    })
  },
  bindRegionChange2(e) {
    this.setData({
      endTime: e.detail.value
    })
  },
  bindRegionChange3(e) {
    this.setData({
      story: this.data.array[e.detail.value]
    })
  },
  showD(e) {
    let {
      id
    } = e.currentTarget.dataset
    getBookingInfo({ shopToken: wx.getStorageSync('shopToken'), bookingId: id }).then(res => {
      if (res.code == 200) {
        this.setData({
          bookInfo: res.data
        })
      }
    })
    this.setData({
      showDetail: true
    })
  },
  close() {
    this.setData({
      showDetail: false
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getBookingList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})